﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ManagedLZO;

namespace mcu_compress
{
    public partial class Form1 : Form
    {
        const int BLOCK_SIZE = 4096;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                textBox1.Text = filePath;
                FileInfo fi = new FileInfo(filePath);
                int filesize = (int)fi.Length;
                
                BinaryReader sr = new BinaryReader(new FileStream(filePath, FileMode.Open));
                BinaryWriter sw = new BinaryWriter(new FileStream(filePath + ".cp", FileMode.Create));

                byte[] fileBytes = new byte[filesize];
                fileBytes = sr.ReadBytes(filesize);
                byte[] dst;

                for (int i = 0; i < filesize;i += BLOCK_SIZE)
                {
                    if ((i + BLOCK_SIZE) < filesize)
                    {
                        byte[] src = new byte[BLOCK_SIZE];

                        Array.Copy(fileBytes, i, src, 0, BLOCK_SIZE);
                        MiniLZO.Compress(src, out dst);
                        sw.Write((byte)(dst.Length));
                        sw.Write((byte)(dst.Length / 256));
                        sw.Write((byte)(src.Length));
                        sw.Write((byte)(src.Length / 256));
                        sw.Write(dst);
                    } 
                    else
                    {
                        int lastBlockSize = filesize - i;
                        byte[] src = new byte[lastBlockSize];

                        Array.Copy(fileBytes, i, src, 0, lastBlockSize);
                        MiniLZO.Compress(src, out dst);
                        sw.Write((byte)(dst.Length));
                        sw.Write((byte)(dst.Length / 256));
                        sw.Write((byte)(src.Length));
                        sw.Write((byte)(src.Length / 256));
                        sw.Write(dst);
                    }
                }

                sr.Close();
                sw.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                textBox1.Text = filePath;
                FileInfo fi = new FileInfo(filePath);
                int filesize = (int)fi.Length;

                BinaryReader sr = new BinaryReader(new FileStream(filePath, FileMode.Open));
                BinaryWriter sw = new BinaryWriter(new FileStream(filePath + ".dc", FileMode.Create));

                byte[] fileBytes = new byte[filesize];
                fileBytes = sr.ReadBytes(filesize);

                int inlen;
                int outlen;

                for (int i = 0; i < filesize;)
                {
                    inlen = fileBytes[i] + fileBytes[i + 1] * 256;
                    outlen = fileBytes[i + 2] + fileBytes[i + 3] * 256;

                    byte[] src = new byte[inlen];
                    Array.Copy(fileBytes, i + 4, src, 0, inlen);

                    byte[] dst = new byte[outlen];
                    MiniLZO.Decompress(src, dst);
                    sw.Write(dst);

                    i = i + 4 + inlen;
                }

                sr.Close();
                sw.Close();
            }
        }
    }
}